Import-Module NTFSSecurity
Import-Module ActiveDirectory
#Load the Exchange cmdlets
$mailsession = New-PSSession  –ConfigurationName microsoft.exchange `
-ConnectionUri http://mailserver.domain.internal/powershell
Import-PSsession $mailsession 

<#Import the Lync module 2010
$lyncOptions = New-PSSessionOption -SkipRevocationCheck -SkipCACheck -SkipCNCheck
$lync = New-PSSession -ConnectionUri https://lyncurl `
-SessionOption $lyncOptions `
-Authentication NegotiateWithImplicitCredential
Import-PSSession $lync
#>
new-alias -name gau -value get-aduser
new-alias -name sau -value set-aduser
new-alias -name gag -value get-adgroup
new-alias -name agm -value Add-ADGroupMember
new-alias -name rgm -value Remove-ADGroupMember
new-alias -name rh -value read-host
new-alias -name wh -value write-host
new-alias -name tp -value test-path
function ck ($user, $detail = "manager")
{
	if($user -like "*com.au"){wh "Please user `"ckm`" function to check mailbox property"
	break}
	$rst=""
	if("mail" -ieq $detail){$rst=(Get-ADUser $user -server $dc -Properties EmailAddress).EmailAddress}
	else{
		if($detail -ieq "m"){$rst=(Get-ADUser $user -server $dc -Properties homedirectory).homedirectory}
		else{$rst=(Get-ADUser $user -server $dc -Properties $detail).$detail}
	}
	if($rst -ne $null -and $rst.gettype().name -eq "DateTime"){$rst="+"+$rst.ToString('MM/dd/yyyy hh:mm:ss tt')}
	return $rst
}
###################################
function ckg ($group, $detail = "ManagedBy")
{
$rst=""
if($group -like "EE\*"){$rst=(Get-ADGroup $group.split("\")[1]  -Properties $detail).$detail}
else{$rst= (Get-ADGroup $group  -Properties $detail).$detail}
if($rst -ne $null -and $rst.gettype().name -eq "DateTime"){$rst="+"+$rst.ToString('MM/dd/yyyy hh:mm:ss tt')}
return $rst
}
###################################
function ckm ($mail, $detail = "IsMailboxEnabled")
{
$rst = (Get-Mailbox $mail).$detail
if($rst -ne $null -and $rst.gettype().name -eq "DateTime"){$rst="+"+$rst.ToString('MM/dd/yyyy hh:mm:ss tt')}
return $rst
}

###################################
function id ($givenName,$Surname= $null)
{
	$id=$null
	if($givenName -like "*com.au"){
		$result = get-mailbox $givenName -ErrorAction SilentlyContinue
		if($result -eq $null){echo "The email doesn't exist"
		$id="Notexist"}
		else{(Get-Mailbox $givenName |Select-Object Alias).Alias
		$id="exist"}
	}
	elseif ($Surname -eq $null -and $givenName.gettype().name -eq "String"){
		if($givenName -like "*,*"){
			$Surname = $givenName.trim().split(",")[-1]
			$givenName = $givenName.trim().split(",")[0]
		}else{
			$Surname = $givenName.trim().split(" ")[-1].trim()
			$givenName = $givenName.trim().split(" ")[0].trim()
		}
	}
	elseif($Surname -eq $null){
		if($givenName -like "*,*"){
			$Surname = $givenName[1].trim()
			$givenName = $givenName[0].trim()
		}else{
			$Surname = $givenName[1].trim()
			$givenName = $givenName[0].trim()
		}
	}
	if ($id -eq $null){
		$Surname = $Surname.trim()
		# $id =(Get-ADUser -Filter {givenName -eq $givenName -and Surname -eq $Surname} -SearchBase "DC=domain,DC=internal").SamAccountName #-ErrorAction SilentlyContinue
		$id =(Get-ADUser -Filter {givenName -eq $givenName -and Surname -eq $Surname}).SamAccountName #-ErrorAction SilentlyContinue
		if ($id -eq $null)
		{(Get-ADUser -Filter {givenName -eq $Surname -and Surname -eq $givenName}).SamAccountName}
		else{echo $id}
	}
}
###################################
function ckUserDetails ()
{
	param
	(
		[Parameter(Mandatory=$true)]
		$property
	)
$erroractionpreference = "SilentlyContinue" 
clear-content .\userid.txt
ii .\userid.txt
rh "please press enter to continue"
$detail=@()
if("mail" -eq $property){ $property="EmailAddress"}
foreach ($ID in  Get-Content .\userid.txt)
	{
		$rst=(Get-ADUser $ID -Properties $property).$property
		if($rst -eq $null){$rst='N/A'}
		elseif($rst.gettype().name -eq "DateTime"){$rst="+"+$rst.ToString('MM/dd/yyyy hh:mm:ss tt')}
		if($property -ine "info"){
			$rst = -join($rst.split("`n"))
		}
		$detail+=$rst
		$rst=$null
	}
	echo $detail >.\userdetails.txt
	ii .\userdetails.txt
}
###################################
function ckf()
{
	param
	(
		[Parameter(Mandatory=$true)]
		$folderpath,
		[Parameter(Mandatory=$false)]
		$type
	)
if (test-path $folderpath){
$obj_new= @()
$erroractionpreference = "SilentlyContinue" 
	$obj=Get-NTFSAccess $folderpath
	foreach ($item in $obj)
	{
	$separator = "EE\\"
	$item_new = ($item.account.AccountName -split $separator)[-1]
	if (get-adgroup $item_new)
		{
		$obj_new+=$item}
	}
	if($type -eq $null){echo $obj_new |select-object Account,Accessrights}
	elseif($type -ieq "rw"){echo $obj_new |select-object Account,Accessrights | ?{$_.Accessrights -eq "Modify, Synchronize"}}
	elseif($type -ieq "ro"){echo $obj_new |select-object Account,Accessrights | ?{$_.Accessrights -eq "ReadAndExecute, Synchronize"}}
	elseif($type -ne $null -and $type -ne "ro" -and $type -ne "rw"){accesschk -d $type $folderpath}
	}
else{echo "The folder $folderpath doesn't exist"}
}
###################################
Function Extend-Aduser()
{
	param
	(
		[Parameter(Mandatory=$true)]
		$ticket,
		[Parameter(Mandatory=$true)]
		$User,
		[DateTime]$Expdate=(Get-Date).AddDays(365) #"MM/dd/yyyy"
	)
$erroractionpreference = "SilentlyContinue" 
$null = get-aduser $User
	if($? -eq $false)
		{
		write-host -foregroundcolor yellow "No such user was found in AD"
		break
		}
Unlock-ADAccount -server $DC –Identity $User
Set-Aduser -server $DC -Identity $User -Enabled $True -AccountExpirationDate $Expdate
$Newinfo = (get-Aduser $User -Properties info).info + " `r`n" +$ticket +" - Extend User Account to "  +$Expdate.ToString('MM/dd/yyyy') + " - Actioned on " +  (get-date).ToString('MM/dd/yyyy')
Set-Aduser $User -Replace @{'info'=$Newinfo }
}
###################################
Function Reset-ADAccountPassword()
{
param(
	[Parameter(Mandatory=$true)]
	$User,$answer ="Y")
$erroractionpreference = "SilentlyContinue" 
$null = get-aduser $User
if($? -eq $false)
	{
	write-host -foregroundcolor yellow "No such user was found in AD"
	break
	}
$pwd= read-host "Please enter temp Password" -assecurestring
Unlock-ADAccount -server $DC –Identity $User
Set-ADAccountPassword -Reset -NewPassword $Pwd  -server $DC –Identity $User
Set-Aduser -server $DC -Identity $User -Enabled $True  -ChangePasswordAtLogon $true
if($answer -ieq "N")
	{
	Set-Aduser -server $DC -Identity $User  -ChangePasswordAtLogon $false
	}
}
###################################
function Lync-Enable($ID)
{
$mail = ck $ID mail
if ($mail  -eq $null){
wh -foregroundcolor yellow "The user doesn't have mailbox!"
break
}
$SipAddress = "sip:" + $mail
Enable-CsUser  -Identity $ID -RegistrarPool lyncpool.truenergy.com.au -SipAddress $SipAddress 
Set-CsUser -Identity $ID -AudioVideoDisabled $True
Get-CsUser $ID|select RegistrarPool,enabled,DisplayName
}
###################################
Function Agrm()
{
	param
	(
		[Parameter(Mandatory=$true)]
		$groupID,
		[Parameter(Mandatory=$true)]
		$user
	)
if($groupID -like "EE\*"){Add-ADGroupMember -Identity $groupID.split("\")[1] -Member $user}
else{Add-ADGroupMember -Identity $groupID -Member $user}
}
function cku_addedgroup($user)
{
Get-ADPrincipalGroupMembership $user |Sort-Object -Property SamAccountName | select SamAccountName
}
###################################
function mail($ID)
{
try{get-aduser  $ID -server $DC > $null
}catch{ wh -foregroundcolor red "The ID does not exist!!!" 
return
}finally{}
$erroractionpreference = "SilentlyContinue" 
$mail = ck $ID mail
if($mail -ne $null){
	wh -foregroundcolor yellow "user already has email $mail"
	return
}

Enable-Mailbox -Identity $ID -Alias $ID -Database $MDB  -ActiveSyncMailboxPolicy 'Default'
Enable-Mailbox -Identity $ID -Archive -ArchiveDatabase $ARC
Set-CASMailbox -Identity $ID  -OWAEnabled $false
<#enable lync access
$mail = ck $ID mail
$SipAddress = "sip:" + $mail
Enable-CsUser  -Identity $ID -RegistrarPool lyncpool.truenergy.com.au -SipAddress $SipAddress 
Set-CsUser -Identity $ID -AudioVideoDisabled $True
#>
}
###################################
function GetMail-AccessType()
{
	param
	(
		[Parameter(Mandatory=$true)]
		$mail,
		[Parameter(Mandatory=$false)]
		$type = "send"
	)
	if ($type -eq "send"){
	echo "Below users have send access to mailbox $mail `r`n" >temp.txt
	(Get-Mailbox $mail  | Select-Object  GrantSendOnBehalfTo).GrantSendOnBehalfTo |foreach-object {$_.Split("/")[-1]} | Sort-Object {$_} -Descending >> temp.txt
	ii .\temp.txt}
	if ($type -ne "send"){
	Get-MailboxPermission $mail| ? {$_.user.tostring() -like "EE\*" -and $_.IsInherited -eq $false -and $_.AccessRights -like "FullAccess*"} | Select Identity,User,AccessRights| Export-Csv -NoTypeInformation fullaccess.csv
	ii .\fullaccess.csv}
}
###################################
$DC='xxxxx.domain.internal'

$MailsLink = @{`
"EAMail1" = "NotesMail1";`
"EAMail2" = "NotesMail2"
}
$EAMail = ck $env:username mail
$NotesMail = $MailsLink.$EAMail 
